<?php

namespace app\service;

use app\ErrorCode;
use app\exception\ApiException;
use app\model\DepartmentManager;
use app\traits\Singleton;

class DepartmentManagerService
{
    use Singleton;

    public function getDepartmentManagerList($departmentId, $leaderName, $leaderPhone, $leaderType, $page, $pageSize)
    {
        $model = DepartmentManager::where("is_delete", "=", 0)
            ->order("id", "desc");

        if ($departmentId) {
            $model->where("department_id", $departmentId);
        }

        if ($leaderName) {
            $model->where("leader_name", "like", "%" . $leaderName . "%");
        }

        if ($leaderPhone) {
            $model->where("leader_phone", $leaderPhone);
        }

        if ($leaderType) {
            $model->where("leader_type", $leaderType);
        }

        $paginator = $model->page($page)
            ->paginate($pageSize);

        $data = array_map(function ($item) {
            return $item;
        }, $paginator->getCollection()->toArray());

        return [
            "list" => $data,
            "total" => $paginator->total(),
        ];
    }

    public function getDepartmentManagerInfo($id)
    {
        return DepartmentManager::where("id", $id)
            ->where("is_delete", 0)
            ->find();
    }

    public function createDepartmentManager($departmentId, $leaderName, $leaderPhone, $leaderType)
    {
        $departmentInfo = DepartmentService::getInstance()->getDepartmentInfo($departmentId);
        if (!$departmentInfo) {
            throw new ApiException("部门不存在", ErrorCode::BUSINESS);
        }
        return DepartmentManager::create([
            "department_id" => $departmentId,
            "leader_name" => $leaderName,
            "leader_phone" => $leaderPhone,
            "leader_type" => $leaderType,
        ]);
    }

    public function updateDepartmentManager($id, $departmentId, $leaderName, $leaderPhone, $leaderType)
    {
        $departmentInfo = DepartmentService::getInstance()->getDepartmentInfo($departmentId);
        if (!$departmentInfo) {
            throw new ApiException("部门不存在", ErrorCode::BUSINESS);
        }

        $departmentManagerInfo = $this->getDepartmentManagerInfo($id);
        if (!$departmentManagerInfo) {
            throw new ApiException("部门负责人不存在", ErrorCode::BUSINESS);
        }

        return DepartmentManager::update([
                "department_id" => $departmentId,
                "leader_name" => $leaderName,
                "leader_phone" => $leaderPhone,
                "leader_type" => $leaderType,
            ], [
                "id" => $id
            ]);
    }

    public function deleteDepartmentManager($id)
    {
        $departmentManagerInfo = $this->getDepartmentManagerInfo($id);
        if (!$departmentManagerInfo) {
            throw new ApiException("部门负责人不存在", ErrorCode::BUSINESS);
        }
        return DepartmentManager::update([
            "is_delete" => 1,
            "delete_time" => time(),
        ], [
            "id" => $id
        ]);
    }
}